// ============================================================================
//
// Copyright (C) 2009-2011 SC Technology Group. - www.sc.com
//
// ============================================================================
package com.sc.vessel.info.editor;

import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.FormToolkit;

import com.cs.vessel.breakdown.Activator;
import com.ibm.team.ui.editor.TeamFormPart;
import com.sc.vessel.info.model.breakdown.BreakdownPackage;

/**
 * DOC liaoqc class global comment. Detailled comment
 */
public class DescriptionPart extends TeamFormPart {

    private InfoInput infoInput;

    @SuppressWarnings("unused")
    private final int fMinLinesSummary;

    private final int fMinLinesDescription;

    @SuppressWarnings("unused")
    private final int fMaxSummarySize;

    private TextViewer fDescription;

    private int ATTRIBUTE_IDENTIFY;

    public DescriptionPart(int minLinesSummary, int minLinesDescription, int maxSummarySize, int accidentPhenomenon) {
        fMinLinesSummary = minLinesSummary;
        fMinLinesDescription = minLinesDescription;
        fMaxSummarySize = maxSummarySize;
        ATTRIBUTE_IDENTIFY = accidentPhenomenon;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.team.ui.editor.TeamFormPart#setInput(java.lang.Object)
     */
    @Override
    public void setInput(Object input) {
        infoInput = (InfoInput) input;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.team.ui.editor.TeamFormPart#createContent(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createContent(Composite container) {

        container.setLayout(new GridLayout());
        // Composite container = new WidthConstrainedComposite(parent, SWT.NULL);
        container.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL
                | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
        FormToolkit toolkit = getSite().getToolkit();
        toolkit.paintBordersFor(container);

        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginTop = 3;
        layout.marginBottom = 5;
        layout.verticalSpacing = 5;
        container.setLayout(layout);

        createContents(container, toolkit);
    }

    /**
     * DOC liaoqc Comment method "createContents".
     * 
     * @param container
     * @param toolkit
     */
    private void createContents(Composite parent, FormToolkit toolkit) {
        toolkit.paintBordersFor(parent);
        // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        // parent.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
        Composite descriptionContainer = new Composite(parent, SWT.NONE);
        descriptionContainer.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL
                | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

        // descriptionContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_YELLOW));
        FillLayout fillLayout = new FillLayout();
        fillLayout.marginWidth = 2;
        descriptionContainer.setLayout(fillLayout);
        descriptionContainer.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);

        fDescription = new TextViewer(descriptionContainer, fMinLinesDescription <= 1 ? SWT.SINGLE : SWT.MULTI | SWT.V_SCROLL
                | SWT.WRAP);
        Control control = fDescription.getControl();
        // descriptionContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        toolkit.adapt(control, false, false);
        GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL
                | GridData.GRAB_VERTICAL);
        if (fMinLinesDescription > 1) {
            data.heightHint = fDescription.getTextWidget().getLineHeight() * fMinLinesDescription;
        }

        IHyperlinkPresenter hyperlinkPresenter = new DefaultHyperlinkPresenter(Activator.getDefault().getPreferenceStore());
        fDescription.setHyperlinkPresenter(hyperlinkPresenter);
        IHyperlinkDetector detector = new URLHyperlinkDetector();
        fDescription.setHyperlinkDetectors(new IHyperlinkDetector[] { detector }, SWT.NONE);

        PresentationReconciler reconciler = new PresentationReconciler();
        reconciler.install(fDescription);

        // set the value
        setValue();

        fDescription.getTextWidget().addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                String text = fDescription.getTextWidget().getText();
                switch (ATTRIBUTE_IDENTIFY) {
                case BreakdownPackage.ACCIDENT__PHENOMENON:
                    infoInput.getAccident().setPhenomenon(text);
                    break;
                case BreakdownPackage.ACCIDENT__PROCESS:
                    infoInput.getAccident().setProcess(text);
                    break;
                case BreakdownPackage.ACCIDENT__ANALYZE:
                    infoInput.getAccident().setAnalyze(text);
                    break;
                case BreakdownPackage.ACCIDENT__EXPERIENCE:
                    infoInput.getAccident().setExperience(text);
                    break;
                default:
                    break;
                }
                DescriptionPart.this.notifyListeners(text);
            }
        });
        data.widthHint = 1;
        descriptionContainer.setLayoutData(data);
    }

    /**
     * DOC liaoqc Comment method "setValue".
     */
    private void setValue() {
        switch (ATTRIBUTE_IDENTIFY) {
        case BreakdownPackage.ACCIDENT__PHENOMENON:
            fDescription.getTextWidget().setText(
                    infoInput.getAccident().getPhenomenon() == null ? "" : infoInput.getAccident().getPhenomenon());
            break;
        case BreakdownPackage.ACCIDENT__PROCESS:
            fDescription.getTextWidget().setText(
                    infoInput.getAccident().getProcess() == null ? "" : infoInput.getAccident().getProcess());
            break;
        case BreakdownPackage.ACCIDENT__ANALYZE:
            fDescription.getTextWidget().setText(
                    infoInput.getAccident().getAnalyze() == null ? "" : infoInput.getAccident().getAnalyze());
            break;
        case BreakdownPackage.ACCIDENT__EXPERIENCE:
            fDescription.getTextWidget().setText(
                    infoInput.getAccident().getExperience() == null ? "" : infoInput.getAccident().getExperience());
            break;
        default:
            break;
        }
    }

    /**
     * DOC liaoqc Comment method "notifyListeners".
     * 
     * @param phenomenon
     */
    protected void notifyListeners(String phenomenon) {
        HandlerEvent handlerEvent = new HandlerEvent(this, true, false);
        Object[] listeners = getListeners();
        for (int i = 0; i < listeners.length; i++) {
            Object listener = listeners[i];
            if (listener instanceof IHandlerListener) {
                IHandlerListener handlerListener = (IHandlerListener) listener;
                handlerListener.handlerChanged(handlerEvent);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#fireHandlerChanged(org.eclipse.core.commands.HandlerEvent)
     */
    @Override
    protected void fireHandlerChanged(HandlerEvent handlerEvent) {
        // TODO Auto-generated method stub
        // super.fireHandlerChanged(handlerEvent);
    }

}
